/**
 * 记牌器
 */

import { CARD, PokerCard } from "../poker/card";
import { PokerSet } from "../poker/set";

export class cardNameRecorder {

  private dic: Map<string, number>;

  constructor() {
    this.dic = new Map<string, number>();
    this.dic.set(CARD.SYMBOL.THREE, 0);
    this.dic.set(CARD.SYMBOL.FOUR, 0);
    this.dic.set(CARD.SYMBOL.FIVE, 0);
    this.dic.set(CARD.SYMBOL.SIX, 0);
    this.dic.set(CARD.SYMBOL.SEVEN, 0);
    this.dic.set(CARD.SYMBOL.EIGHT, 0);
    this.dic.set(CARD.SYMBOL.NINE, 0);
    this.dic.set(CARD.SYMBOL.TEN, 0);
    this.dic.set(CARD.SYMBOL.JACK, 0);
    this.dic.set(CARD.SYMBOL.QUEEN, 0);
    this.dic.set(CARD.SYMBOL.KING, 0);
    this.dic.set(CARD.SYMBOL.ACE, 0);
    this.dic.set(CARD.SYMBOL.TWO, 0);
    this.dic.set(CARD.SYMBOL.BLACK_JOKER, 0);
    this.dic.set(CARD.SYMBOL.RED_JOKER, 0);
  }

  /**
   * 增加记录器
   * @param playerPokers
   */
  public AddPokerSet(playerPokers: Array<PokerSet>) {
    for (const pokerSet of playerPokers) {
      pokerSet.DoOnEachPokerCard((index: number, card: PokerCard) => {
        const count = this.dic.get(card.GetCardName())!;
        this.dic.set(card.GetCardName(), count + 1);
      })
    }
  }

  /**
   * 更新玩家记牌器, 玩家出牌后调用
   * @param cards
   */
  public RemovePokerSet(cards: PokerSet) {
    cards.DoOnEachPokerCard((index: number, card: PokerCard) => {
      const count = this.dic.get(card.GetCardName())!;
      this.dic.set(card.GetCardName(), count - 1);
    })
  }

  /**
   * 根据牌的顺序从大到小排序的记牌器json对象
   */
  public SequenceJsonEncode(): string {
    let jsonString = "";
    jsonString += "{";
    jsonString += "'" + CARD.SYMBOL.RED_JOKER + "'" + ":" + String(this.getPokerNum(CARD.SYMBOL.RED_JOKER));
    jsonString += ",";
    jsonString += "'" + CARD.SYMBOL.BLACK_JOKER + "'" + ":" + String(this.getPokerNum(CARD.SYMBOL.BLACK_JOKER));
    jsonString += ",";
    jsonString += "'" + CARD.SYMBOL.TWO + "'" + ":" + String(this.getPokerNum(CARD.SYMBOL.TWO));
    jsonString += ",";
    jsonString += "'" + CARD.SYMBOL.ACE + "'" + ":" + String(this.getPokerNum(CARD.SYMBOL.ACE));
    jsonString += ",";
    jsonString += "'" + CARD.SYMBOL.KING + "'" + ":" + String(this.getPokerNum(CARD.SYMBOL.KING));
    jsonString += ",";
    jsonString += "'" + CARD.SYMBOL.QUEEN + "'" + ":" + String(this.getPokerNum(CARD.SYMBOL.QUEEN));
    jsonString += ",";
    jsonString += "'" + CARD.SYMBOL.JACK + "'" + ":" + String(this.getPokerNum(CARD.SYMBOL.JACK));
    jsonString += ",";
    jsonString += "'" + CARD.SYMBOL.TEN + "'" + ":" + String(this.getPokerNum(CARD.SYMBOL.TEN));
    jsonString += ",";
    jsonString += "'" + CARD.SYMBOL.NINE + "'" + ":" + String(this.getPokerNum(CARD.SYMBOL.NINE));
    jsonString += ",";
    jsonString += "'" + CARD.SYMBOL.EIGHT + "'" + ":" + String(this.getPokerNum(CARD.SYMBOL.EIGHT));
    jsonString += ",";
    jsonString += "'" + CARD.SYMBOL.SEVEN + "'" + ":" + String(this.getPokerNum(CARD.SYMBOL.SEVEN));
    jsonString += ",";
    jsonString += "'" + CARD.SYMBOL.SIX + "'" + ":" + String(this.getPokerNum(CARD.SYMBOL.SIX));
    jsonString += ",";
    jsonString += "'" + CARD.SYMBOL.FIVE + "'" + ":" + String(this.getPokerNum(CARD.SYMBOL.FIVE));
    jsonString += ",";
    jsonString += "'" + CARD.SYMBOL.FOUR + "'" + ":" + String(this.getPokerNum(CARD.SYMBOL.FOUR));
    jsonString += ",";
    jsonString += "'" + CARD.SYMBOL.THREE + "'" + ":" + String(this.getPokerNum(CARD.SYMBOL.THREE));
    jsonString += "}";
    return jsonString;
  }

  private getPokerNum(key: string): number {
    const count = this.dic.get(key);
    if (!!count) {
      return count;
    }
    return 0;
  }
}
